package com.starinside.wp.controller.api;

import com.google.common.collect.ImmutableMap;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.TeachingPlanVo;
import com.starinside.wp.service.ChildrenCourseService;
import com.starinside.wp.utils.RemoteCallUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Describle This Class Is Course Page
 * @Author ZengMin
 * @Date 2018/11/12 17:40
 * @Company Matt
 */
@RequestMapping("/wx/api/course")
@Api(value="/wx/api/course", tags = "用户课程")
@RestController
public class APIChildrenCourseController {

    @Value("${teach.server.url}")
    String teachUrl;

    @Autowired
    ChildrenCourseService childrenCourseService;

    private static final String COURSE_INFO_URI = "/api/phone/user/children/getChildrenCourse";

    /**
     * @return 课程概览
     */
    @ApiOperation("课程概览")
    @RequestMapping(value = "/getChildrenCourse", method = RequestMethod.GET)
    public StatusResponse getChildrenCourseInfo(HttpServletRequest req, HttpServletResponse resp){

        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        String userId = user.getParentId();

        List<HashMap> hashMaps = RemoteCallUtil.callOfList(teachUrl + COURSE_INFO_URI, ImmutableMap.of("userId", userId), null, HashMap.class, "教务系统 通讯异常");
        return StatusResponse.success(hashMaps);
    }

    /**
     * @param addDay
     * @return 成长方案
     */
    @ApiOperation("成长方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addDay", value = "范围", defaultValue = "0", required = false)
    })
    @RequestMapping(value = "/getGrowPlan", method = RequestMethod.GET)
    public StatusResponse getGrowPlan(HttpServletRequest req, HttpServletResponse resp, @RequestParam(required = false,defaultValue = "0") int addDay){
        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");


        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        String childrenId = user.getDefaultBabyId();

        List<TeachingPlanVo> growPlan = childrenCourseService.getGrowPlan(childrenId, addDay);
        return StatusResponse.success(growPlan);
    }

    /**
     * @param materialId
     * @return 成长方案详情
     */
    @ApiOperation("成长方案详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialId", value = "材料 id")
    })
    @RequestMapping(value = "/getGrowPlanInfo", method = RequestMethod.GET)
    public StatusResponse getGrowPlan(@RequestParam(required = true) long materialId){
        String path = teachUrl + "/schemeDetail/%ticket%/" + materialId;
        Map<String,Object> map = new HashMap<>();
        map.put("materialId",materialId);
        map.put("url",path);
        return StatusResponse.success(map);
    }

    //评测列表
    @ApiOperation("评测列表")
    @RequestMapping(value = "/getChildEvaluatingList", method = RequestMethod.GET)
    public StatusResponse getChildEvaluatingList(HttpServletRequest req, HttpServletResponse resp){
        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");


        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        String childrenId = user.getDefaultBabyId();


        return childrenCourseService.getChildEvaluatingList(childrenId);
    }

    //早教课程列表
    @ApiOperation("早教课程列表")
    @RequestMapping(value = "/getSatCourse", method = RequestMethod.GET)
    public StatusResponse getSatCourse(HttpServletRequest req, HttpServletResponse resp){
        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");


        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        String childrenId = user.getDefaultBabyId();

        return childrenCourseService.getSatCourse(childrenId);
    }
}
